package com.api.demo.controller;

import com.api.demo.entity.Content;
import com.api.demo.entity.User;
import com.api.demo.service.ContentRecommendService;
import com.api.demo.service.UserService;
import com.api.demo.vo.ContentVO;
import com.api.demo.vo.RecommendResponse;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/recommend")
public class RecommendController {

    @Autowired
    private ContentRecommendService recommendService;

    @Autowired
    private UserService userService;

    @GetMapping("/contents")
    public RecommendResponse getRecommendContents(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            Authentication authentication) {

        // 创建分页对象
        Page<Content> pageParam = new Page<>(page, size);

        // 获取推荐内容
        IPage<Content> contentPage = recommendService
                .getRecommendations(getUserIdFromUsername(authentication.getName()), pageParam);

        // 转换为VO对象
        List<ContentVO> contentVOs = contentPage.getRecords().stream()
                .map(this::convertToVO)
                .collect(Collectors.toList());

        // 构建响应对象
        return new RecommendResponse(
                contentVOs,
                contentPage.getCurrent(),
                contentPage.getSize(),
                contentPage.getTotal(),
                contentPage.getPages());
    }

    @PostMapping("/view/{contentId}")
    public void recordView(
            @PathVariable Long contentId,
            Authentication authentication) {

        recommendService.recordContentView(getUserIdFromUsername(authentication.getName()), contentId);
    }

    @GetMapping("/follow")
    public RecommendResponse getFollowContent(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int size,
            Authentication authentication) {
        Page<Content> pageParam = new Page<>(page, size);
        IPage<Content> contentPage = recommendService.getFollowContent(getUserIdFromUsername(authentication.getName()),
                pageParam);
        List<ContentVO> contentVOs = contentPage.getRecords().stream()
                .map(this::convertToVO)
                .collect(Collectors.toList());

        // 构建响应对象
        return new RecommendResponse(
                contentVOs,
                contentPage.getCurrent(),
                contentPage.getSize(),
                contentPage.getTotal(),
                contentPage.getPages());
    }

    private ContentVO convertToVO(Content content) {
        ContentVO vo = new ContentVO();
        BeanUtils.copyProperties(content, vo);
        // 设置内容类型对应的icon
        if (content.getType() == 1) {
            vo.setTypeIcon("icon-article");
        } else if (content.getType() == 2) {
            vo.setTypeIcon("icon-video");
        }
        return vo;
    }

    private Long getUserIdFromUsername(String username) {
        User user = userService.findByUsername(username);
        if (user == null) {
            throw new IllegalArgumentException("用户不存在");
        }
        return user.getId();
    }
}